import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerBean;

import test.clone.testClass;
import test.message.TestEventParam;
import test.message.TestMassageListener;
import test.message.TestMessageEvent;
import test.schedule.quartz.NewJobTarget;


public class SpringConsoleMain {

	
	/**
	 * @param args
	 * @throws NoSuchMethodException 
	 * @throws ClassNotFoundException 
	 * @throws ParseException 
	 * @throws SchedulerException 
	 */
	public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, ParseException, SchedulerException {
		List<testClass> itemList = new ArrayList<testClass>();
		testClass soloItem = new testClass();
		org.springframework.scheduling.quartz.SimpleTriggerBean a = new SimpleTriggerBean();
		a.setRepeatInterval(100);
		
		soloItem.setParentA("P A");
		soloItem.setA("Main Key");
		
		for(int i = 0;i<10 ;i++)
		{
			soloItem.setParentB("P " + i);
			soloItem.setB(i);
			itemList.add((testClass) soloItem.clone());
		}
		
		for(testClass item : itemList)
		{
			System.out.println(item);
		}
		
		ApplicationContext context = new ClassPathXmlApplicationContext("*Context.xml");
		
		TestMassageListener bean = context.getBean(TestMassageListener.class);
		bean.HelloIam();
		for(String beanName : context.getBeanDefinitionNames())
		{
			System.out.println(beanName);
		}
		
		TestEventParam srcItem = new TestEventParam(31,"SengWook","M");
		context.publishEvent(new TestMessageEvent("testEvent01", srcItem));
		
		System.out.println("It's Main");
		
		try {

			InputStreamReader converter = new InputStreamReader(System.in);
			BufferedReader in = new BufferedReader(converter);
			String CurLine = "";
			int counter = 100;
			while (!(CurLine.equals("quit"))) 
			{
				CurLine = in.readLine();
					
				if (!(CurLine.equals("quit"))) {
					System.out.println("You typed: " + CurLine);
					Scheduler sch = (Scheduler) context.getBean("schedulerFactory");
					
					NewJobTarget nj = (NewJobTarget) context.getBean("newJobTarget");
					if(sch == null)
					{
						System.out.println("SCHEDULER IS NULL");
					}
					if(nj == null)
					{
						System.out.println("NewJob IS NULL");
					}
					MethodInvokingJobDetailFactoryBean mijdf = new MethodInvokingJobDetailFactoryBean();
					mijdf.setTargetObject(new NewJobTarget(counter));
					mijdf.setTargetMethod("doSomeThing");
					mijdf.setName("NEWTASK" + counter);
					mijdf.setConcurrent(false);
					mijdf.afterPropertiesSet();
					
					SimpleTriggerBean trigger = new SimpleTriggerBean();
					trigger.setBeanName("NEWTASK" + counter);
					trigger.setJobDetail((JobDetail) mijdf.getObject());
					trigger.setRepeatInterval(3000);
					trigger.afterPropertiesSet();
					
					sch.scheduleJob((JobDetail) mijdf.getObject(), trigger);
					counter++;
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
